﻿using System;
using System.Windows.Forms;

namespace Administracja
{
    public partial class OsobySkladnikiEdycja : Form
    {
        private readonly int _id;
        private readonly bool _dodawanie;

        public OsobySkladnikiEdycja(int id, bool dodawanie)
        {
            InitializeComponent();
            _id = id;
            _dodawanie = dodawanie;

            Wyswietl();
        }

        private void Wyswietl()
        {
            var reader1D = Baza.Wczytaj1D("SELECT * FROM SKLADNIKI WHERE ID ='" + _id + "'");

            ID_lab.Text = "ID : " + reader1D[0];
            nazwaBox.Text = reader1D[1];
            symbolBox.Text = reader1D[2];

            cenaNettoBox.Text = string.Format("{0:N2}", Convert.ToDouble(reader1D[3].Replace(".", ",")));
            vatBox.Text = reader1D[4];

            LiczBrutto(this, EventArgs.Empty);
        }

        private void LiczBrutto(object sender, EventArgs e)
        {
            try
            {
                double netto = Convert.ToDouble(cenaNettoBox.Text.Replace('.', ','));
                int vat = Convert.ToInt32(vatBox.Text);

                cena_brutto_l.Text = "Cena Brutto : " + string.Format("{0:N2}", netto + netto * vat / 100);
            }
            catch (Exception ex)
            {
                Wyjatki.Error(ex);
            }
        }

        private void ZapiszClick(object sender, EventArgs e)
        {
            Baza.Zapisz("UPDATE SKLADNIKI SET "
                    + "nazwa = '" + nazwaBox.Text
                    + "', symbol = '" + symbolBox.Text
                    + "', cena_netto = '" + cenaNettoBox.Text.Replace(",", ".")
                    + "', vat = '" + vatBox.Text.Replace(",", ".")
                    + "' WHERE ID = " + _id + ";");
            Close();
        }

        private void AnulujClick(object sender, EventArgs e)
        {
            if (_dodawanie)
            {
                Baza.Zapisz("DELETE FROM SKLADNIKI WHERE ID = '" + _id + "'");
                Baza.Ustaw("SKLADNIKI");
            }
        }

        private void CenaNettoBoxClick(object sender, EventArgs e)
        {
            cenaNettoBox.SelectAll();

            try
            {
                vatBox.Text = Convert.ToInt32(vatBox.Text.Replace('.', ',')).ToString();
            }
            catch { }
        }

        private void VatBoxClick(object sender, EventArgs e)
        {
            vatBox.SelectAll();

            try
            {
                cenaNettoBox.Text = Convert.ToDouble(cenaNettoBox.Text.Replace('.', ',')).ToString("0.00");
            }
            catch { }
        }

        private void NazwaBoxTextChanged(object sender, EventArgs e)
        {
            int poz = nazwaBox.SelectionStart;
            try
            {
                nazwaBox.Text = nazwaBox.Text.Substring(0, 1).ToUpper() + nazwaBox.Text.Substring(1);
            }
            catch { }
            nazwaBox.SelectionStart = poz;
        }
    }
}
